/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.check.checkers;

import cn.ac.iie.di.ban.news.server.check.CheckResult;
import cn.ac.iie.di.ban.news.server.check.Checker;

/**
 *
 * @author zwlin
 */
public class EmailChecker implements Checker<String, Object> {

	@Override
	public CheckResult check(String src, Object target) {
		//empty
		if (src == null || (src = src.trim()).isEmpty()) {
			return new CheckResult();
		}
		//@
		if (!src.contains("@")) {
			return new CheckResult("no '@' in the email address.");
		}
		//
		String[] dvd = src.split("@");
		if (dvd.length != 2) {
			return new CheckResult("too many '@' in an email address.");
		}
		if (dvd[0].trim().isEmpty() || (dvd[1] = dvd[1].trim()).isEmpty()) {
			return new CheckResult("one part of email address is empty.");
		}
		if (!dvd[1].contains(".") || dvd[1].startsWith("\\.") || dvd[1].endsWith("\\.")) {
			return new CheckResult("'" + dvd[1] + "' is not a valid email suffix.");
		}
		return new CheckResult();
	}

}
